package com.yq.cns.ui.main.fragment.news

import android.content.Context
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.viewpager2.adapter.FragmentStateAdapter
import base.BaseLazyFragment
import com.fmt.mvi.learn.gobal.log
import com.yq.cns.R
import com.yq.cns.databinding.FragmentNewsBinding
import com.yq.cns.entity.ChannelEntity
import com.yq.cns.ui.main.fragment.common.CommonNewsFragment
import com.yq.cns.utils.ViewPagerHelper
import com.zy.multistatepage.MultiStateContainer
import com.zy.multistatepage.MultiStatePage
import com.zy.multistatepage.state.ErrorState
import com.zy.multistatepage.state.LoadingState
import com.zy.multistatepage.state.SuccessState
import kotlinx.coroutines.launch
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.CommonPagerTitleView

/**
 * @author: user
 * @date: 2022/11/16
 */
class NewsFragment : BaseLazyFragment<FragmentNewsBinding>() {
    var multiStateContainer: MultiStateContainer? = null
    private val mViewModel by lazy { ViewModelProvider(this)[NewsFragmentViewModel::class.java] }

    override fun getLayout() = R.layout.fragment_news

    override fun loadPageData() {
        mViewModel.dispatchEvent(NewsFragmentAction.GetChannelData)
    }

    override fun getViewBinding(
        inflater: LayoutInflater,
        container: ViewGroup?
    ) = FragmentNewsBinding.inflate(inflater,container,false)

    override fun initView() {
        super.initView()
        multiStateContainer = MultiStatePage.bindMultiState(binding.mViewPager)
    }


    override fun initListener() {
        super.initListener()
        lifecycleScope.launch {
            mViewModel.state.collect {
                when (it) {
                    is NewsFragmentState.Loading -> {
                        multiStateContainer?.show(LoadingState::class.java)
                    }
                    is NewsFragmentState.OnSuccess -> {
                        multiStateContainer?.show(SuccessState::class.java)
                        "获取渠道数据成功".log()
                        initChannelIndicator(it.data)
                        initViewPage(it.data)
                    }
                    is NewsFragmentState.Fail ->{
                        it.msg?.log()
                        multiStateContainer?.show(ErrorState::class.java)
                    }
                }
            }
        }
    }

    private fun initChannelIndicator(channels: List<ChannelEntity>) {
        val commonNavigator = CommonNavigator(context)
        commonNavigator.adapter = object : CommonNavigatorAdapter() {
            override fun getCount() = channels.size

            override fun getTitleView(context: Context?, index: Int): IPagerTitleView {
                val titleView = CommonPagerTitleView(context)
                val contentView = TextView(context).apply {
                    gravity = Gravity.CENTER
                    setPadding(10, 0, 10, 0)
                }
                contentView.text = channels[index].name
                titleView.setContentView(contentView)
                titleView.setOnClickListener {
                    binding.mViewPager.setCurrentItem(index, false)
                }
                return titleView
            }

            override fun getIndicator(context: Context?): IPagerIndicator {
                val indicator = LinePagerIndicator(context).apply {
                    mode = LinePagerIndicator.MODE_EXACTLY
                    setColors(resources.getColor(R.color.color_d8413a))
                }
                return indicator
            }

        }
        binding.indicator.navigator = commonNavigator
        ViewPagerHelper.bind(binding.indicator, binding.mViewPager)
    }

    fun initViewPage(channels: List<ChannelEntity>) {
        binding.mViewPager.offscreenPageLimit = channels.size
        binding.mViewPager.adapter = object : FragmentStateAdapter(this) {
            override fun getItemCount() = channels.size

            override fun createFragment(position: Int): Fragment {
                return CommonNewsFragment().apply {
                    initArg(channels[position])
                }
            }
        }
    }


}